package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.mapper.CompanyApplyRecordModelMapper;
import one.stand.mapper.CompanyCategoryRelationModelMapper;
import one.stand.model.*;
import one.stand.service.CompanyApplyService;
import one.stand.service.CompanyBaseService;
import one.stand.service.WebsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by yandou on 2019/6/14.
 */
@Service
public class CompanyApplyServiceImpl implements CompanyApplyService {

    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private WebsiteService websiteService;
    @Autowired
    private CompanyApplyRecordModelMapper companyApplyRecordModelMapper;
    @Autowired
    private CompanyCategoryRelationModelMapper companyCategoryRelationModelMapper;

    @Override
    public ResultModel list(CompanyApplyRecordModel request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyCategoryModel> categoryModelList = companyApplyRecordModelMapper.selectAll(request);
        return ResultModel.success(new PageInfo(categoryModelList));
    }

    @Override
    public ResultModel updateState(CompanyApplyRecordModel request) {
//        request.setCompanyId(request.getCId().intValue());
        request.setUtime(new Date());
        companyApplyRecordModelMapper.updateByPrimaryKeySelective(request);
        // 审核通过处理
        if (request.getAuditState() != null && request.getAuditState() == CompanyApplyRecordModel.audit_success) {
            if (request.getApplyCompanyId() == 0) { // 向平台申请
                companyBaseService.updateQjState(request.getCompanyId(), request.getApplyType() == CompanyApplyRecordModel.apply_type_supply);
            } else { // 向供应商申请代理, 更新被代理次数
                companyBaseService.updateAgentTime(request.getApplyCompanyId());
            }
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel add(CompanyApplyRecordModel request) {
        Company companyModel = companyBaseService.getById(request.getCId().intValue());
        if (companyModel == null || companyModel.getIsDel() != 0) {
            return ResultModel.fail("申请企业状态异常");
        }
        // 获取地址
        Website websiteModel = websiteService.getByCompanyId(companyModel.getCompanyId());
        if (websiteModel != null) {
            request.setAddress(websiteModel.getAddress());
        }
        request.setCompanyId(companyModel.getCompanyId());
        request.setCompanyName(companyModel.getCompanyName());
        request.setCompanyLogo(companyModel.getCompanyLogo());
        request.setPhone(companyModel.getPhone());
        request.setAuditState(0);
        Date date = new Date();
        request.setCreateTime(date);
        request.setUtime(date);
        companyApplyRecordModelMapper.insert(request);
        return ResultModel.success();
    }

    @Override
    public ResultModel categoryList(CompanyCategoryRelationModel request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyCategoryRelationModel> list = companyCategoryRelationModelMapper.select(request);
        return ResultModel.success(new PageInfo(list));
    }

    @Override
    public ResultModel categoryAdd(CompanyCategoryRelationModel request) {
        Date date = new Date();
        request.setCreateTime(date);
        request.setUtime(date);
        companyCategoryRelationModelMapper.insert(request);
        return ResultModel.success();
    }

    @Override
    public ResultModel categoryDel(CompanyCategoryRelationModel request) {
        companyCategoryRelationModelMapper.deleteByPrimaryKey(request.getId().intValue());
        return ResultModel.success();
    }
}
